from typing import List
from .model import UsersCustomer
from ninja import Field, ModelSchema, Query, Router
from ninja.pagination import paginate
from utils.fu_crud import (
    ImportSchema,
    create,
    delete,
    export_data,
    import_data,
    retrieve,
    update,
)
from utils.fu_ninja import FuFilters, MyPagination

router = Router()


# 设置过滤字段
class Filters(FuFilters):
        
    phone_number: str = Field(None, alias='phone_number')    
    partner_name: str = Field(None, alias='partner_name')    
    partner_id: str = Field(None, alias='partner_id')    
    user_group: str = Field(None, alias='user_group')    
    user_name: str = Field(None, alias='user_name')    
    user_id: str = Field(None, alias='user_id')


# 设置请求接收字段
class UsersCustomerSchemaIn(ModelSchema):

    class Config:
        model = UsersCustomer
        model_exclude = ['create_datetime', 'update_datetime', 'belong_dept', 'modifier', 'creator', 'id']


# 设置响应字段
class UsersCustomerSchemaOut(ModelSchema):

    class Config:
        model = UsersCustomer
        model_fields = '__all__'


# 创建UsersCustomer
@router.post('/users_customer', response=UsersCustomerSchemaOut)
def create_users_customer(request, data: UsersCustomerSchemaIn):
    users_customer = create(request, data, UsersCustomer)
    return users_customer


# 删除UsersCustomer
@router.delete('/users_customer/{id}')
def delete_users_customer(request, id: int):
    delete(id, UsersCustomer)
    return {'success': True}


# 更新UsersCustomer
@router.put('/users_customer/{id}', response=UsersCustomerSchemaOut)
def update_users_customer(request, id: int, data: UsersCustomerSchemaIn):
    users_customer = update(request, id, data, UsersCustomer)
    return users_customer


# 获取UsersCustomer
@router.get('/users_customer', response=List[UsersCustomerSchemaOut])
@paginate(MyPagination)
def list_users_customer(request, filters: Filters = Query(...)):
    qs = retrieve(request, UsersCustomer, filters)
    return qs


# 导入
@router.get('/users_customer/all/export')
def export_users_customer(request):
    export_fields = ('phone_number', 'partner_name', 'partner_id', 'user_group', 'user_name', 'user_id', )
    return export_data(request, UsersCustomer, UsersCustomerSchemaOut, export_fields)


# 导出
@router.post('/users_customer/all/import')
def import_users_customer(request, data: ImportSchema):
    import_fields = ('phone_number', 'partner_name', 'partner_id', 'user_group', 'user_name', 'user_id', )
    return import_data(request, UsersCustomer, UsersCustomerSchemaIn, data, import_fields)
    